/* Copyright (c) 2016, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifndef __CAMSCOPE_PACKET_TYPE_H__
#define __CAMSCOPE_PACKET_TYPE_H__

#include <stdint.h>
#include <time.h>

#define CAMSCOPE_OFF_FLAG 0x00000000
#define CAMSCOPE_ON_FLAG 0xFFFFFFFF
#define CAMSCOPE_OFF_MASK 0x00000000
#define CAMSCOPE_KPI_MASK 0x00000001
#define CAMSCOPE_KPI_DBG_MASK 0x00000002
#define CAMSCOPE_ALWAYS_ON_MASK 0xFFFFFFFF

extern volatile uint32_t kpi_camscope_flags;
extern volatile uint32_t kpi_camscope_frame_count;

typedef enum {
    CAMSCOPE_SECTION_MMCAMERA,
    CAMSCOPE_SECTION_HAL,
    CAMSCOPE_SECTION_JPEG,
    CAMSCOPE_SECTION_SIZE,
} camscope_section_type;

typedef enum {
    CAMSCOPE_BASE,
    CAMSCOPE_SOFTWARE_BASE,
    CAMSCOPE_SYNC_BEGIN,
    CAMSCOPE_SYNC_END,
    CAMSCOPE_ASYNC_BEGIN,
    CAMSCOPE_ASYNC_END,
    CAMSCOPE_SYNC_EVENT,
    CAMSCOPE_ASYNC_EVENT,
    CAMSCOPE_RESERVED = 0xFFFFFFFF
} camscope_packet_type;

typedef enum {
    CAMSCOPE_MCT_SOF,
    CAMSCOPE_MCT_SUP_PARAMS,
    CAMSCOPE_MCT_SPEC_EVT,
    CAMSCOPE_MCT_BUS_PROC,
    CAMSCOPE_AFD,
    CAMSCOPE_ASD,
    CAMSCOPE_AEC,
    CAMSCOPE_AWB,
    CAMSCOPE_AF,
    CAMSCOPE_CPP,
    CAMSCOPE_CPP_CAPTURE,
    CAMSCOPE_CPP_CLK_REQ,
    CAMSCOPE_CPP_HOLDING,
    CAMSCOPE_CPP_HW_ON,
    CAMSCOPE_SNAPSHOT,
    CAMSCOPE_ISP_HW_UPDATE,
    CAMSCOPE_JPEG,
    CAMSCOPE_FACEPROC,
    CAMSCOPE_SENSOR_PROCESS,
    CAMSCOPE_FD_NUM_DETECTED,
    CAMSCOPE_CAM_ALLOC,
    CAMSCOPE_IFACE_STREAMON_FWD,
    CAMSCOPE_IFACE_STREAMON_THREAD,
    CAMSCOPE_IFACE_STREAMOFF_FWD,
    CAMSCOPE_IFACE_STREAMOFF_THREAD,
    CAMSCOPE_IFACE_CFG_ISP,
    CAMSCOPE_IFACE_HW_CFG,
    CAMSCOPE_IFACE_CREATE_AXI_HW,
    CAMSCOPE_IFACE_CFG_AXI_HW,
    CAMSCOPE_IFACE_STREAMON,
    CAMSCOPE_IFACE_STREAMOFF,
    CAMSCOPE_AF_START,
    CAMSCOPE_AF_SET,
    CAMSCOPE_IS,
    CAMSCOPE_ISP_STREAMON,
    CAMSCOPE_ISP_STREAMOFF,
    CAMSCOPE_ISP_SET_STRM_CFG,
    CAMSCOPE_VFE_HW_UPDATE,
    CAMSCOPE_ISP_STREAMON_FWD,
    CAMSCOPE_SENSOR_SD_OPEN,
    CAMSCOPE_SENSOR_START_SESSION,
    CAMSCOPE_SENSOR_SET_RESOLUTION,
    CAMSCOPE_SENSOR_SET_STRM_CFG,
    CAMSCOPE_SENSOR_CFG_PDAF,
    CAMSCOPE_SENSOR_LOAD_CHROMATIX,
    CAMSCOPE_SENSOR_START_STREAM,
    CAMSCOPE_SENSOR_SET_FPS,
    CAMSCOPE_SENSOR_STREAMOFF,
    CAMSCOPE_WNR,
    CAMSCOPE_WNR_MEMCPY,
    CAMSCOPE_PPROC_STREAMOFF,
    CAMSCOPE_CPP_STREAMON,
    CAMSCOPE_CAC,
    CAMSCOPE_CPP_CREATE_HW_FRAME,
    CAMSCOPE_CPP_SET_STRM_CFG,
    CAMSCOPE_MCT_START_SESSION,
    CAMSCOPE_MCT_STOP_SESSION,
    CAMSCOPE_IMGLIB_STREAMON,
    CAMSCOPE_MCT_CREATE_BUF,
    CAMSCOPE_HAL1_START_PREVIEW,
    CAMSCOPE_HAL1_STOP_PREVIEW,
    CAMSCOPE_HAL1_TAKE_PICTURE,
    CAMSCOPE_HAL1_CLOSECAMERA,
    CAMSCOPE_HAL1_OPENCAMERA,
    CAMSCOPE_HAL1_STARTPREVIEW,
    CAMSCOPE_HAL1_STOPPREVIEW,
    CAMSCOPE_HAL1_CAPTURE_CH_CB,
    CAMSCOPE_HAL1_PREVIEW_STRM_CB,
    CAMSCOPE_HAL3_SNAPSHOT,
    CAMSCOPE_HAL3_GETSTREAMBUFS,
    CAMSCOPE_HAL3_OPENCAMERA,
    CAMSCOPE_HAL3_CLOSECAMERA,
    CAMSCOPE_HAL3_FLUSH,
    CAMSCOPE_HAL1_ZSL_CH_CB,
    CAMSCOPE_HAL1_PP_CH_CB,
    CAMSCOPE_HAL1_SYNC_STRM_CB,
    CAMSCOPE_HAL1_NODIS_PREVIEW_STRMCB,
    CAMSCOPE_HAL1_RDI_MODE_STRM_CB,
    CAMSCOPE_HAL1_POSTVIEW_STRM_CB,
    CAMSCOPE_HAL1_VIDEO_STRM_CB,
    CAMSCOPE_HAL1_SNAPSHOT_CH_CB,
    CAMSCOPE_HAL1_RAW_STRM_CB,
    CAMSCOPE_HAL1_RAW_CH_CB,
    CAMSCOPE_HAL1_PREVIEW_RAW_STRM_CB,
    CAMSCOPE_HAL1_SNAPSHOT_RAW_STRM_CB,
    CAMSCOPE_HAL1_METADATA_STRM_CB,
    CAMSCOPE_HAL1_REPROC_STRM_CB,
    CAMSCOPE_HAL1_CB_STRM_CB,
    CAMSCOPE_HAL1_SET_PREVIEW_WINDOW,
    CAMSCOPE_HAL1_SET_CALLBACKS,
    CAMSCOPE_HAL1_ENABLE_MSG_TYPE,
    CAMSCOPE_HAL1_DISABLE_MSG_TYPE,
    CAMSCOPE_HAL1_MSG_TYPE_ENABLED,
    CAMSCOPE_HAL1_PREPARE_PREVIEW,
    CAMSCOPE_HAL1_PREVIEW_ENABLED,
    CAMSCOPE_HAL1_RESTART_START_PREVIEW,
    CAMSCOPE_HAL1_RESTART_STOP_PREVIEW,
    CAMSCOPE_HAL1_PRE_START_RECORDING,
    CAMSCOPE_HAL1_START_RECORDING,
    CAMSCOPE_HAL1_STOP_RECORDING,
    CAMSCOPE_HAL1_RECORDING_ENABLED,
    CAMSCOPE_HAL1_REL_REC_FRAME,
    CAMSCOPE_HAL1_CANCEL_AF,
    CAMSCOPE_HAL1_PRE_TAKE_PICTURE,
    CAMSCOPE_HAL1_CANCEL_PICTURE,
    CAMSCOPE_HAL1_SET_PARAMETERS,
    CAMSCOPE_HAL1_STOP_AFTER_SET_PARAMS,
    CAMSCOPE_HAL1_COMMIT_PARAMS,
    CAMSCOPE_HAL1_RESTART_AFTER_SET_PARAMS,
    CAMSCOPE_HAL1_GET_PARAMETERS,
    CAMSCOPE_HAL1_PUT_PARAMETERS,
    CAMSCOPE_HAL1_SEND_COMMAND,
    CAMSCOPE_HAL1_SEND_COMMAND_RESTART,
    CAMSCOPE_HAL1_RELEASE,
    CAMSCOPE_HAL1_REGISTER_FACE_IMAGE,
    CAMSCOPE_HAL1_PREPARE_SNAPSHOT,
    CAMSCOPE_HAL1_QCAMERA2HWI,
    CAMSCOPE_HAL1_INIT_CAP,
    CAMSCOPE_HAL1_GET_CAP,
    CAMSCOPE_HAL1_PREPAREPREVIEW,
    CAMSCOPE_HAL1_PREPARE_HW_FOR_SNAPSHOT,
    CAMSCOPE_HAL3_INIT,
    CAMSCOPE_HAL3_CFG_STRMS,
    CAMSCOPE_HAL3_CFG_STRMS_PERF_LKD,
    CAMSCOPE_HAL3_HANDLE_BATCH_METADATA,
    CAMSCOPE_HAL3_HANDLE_METADATA_LKD,
    CAMSCOPE_HAL3_HANDLE_IN_BUF_LKD,
    CAMSCOPE_HAL3_HANDLE_BUF_LKD,
    CAMSCOPE_HAL3_PROC_CAP_REQ,
    CAMSCOPE_HAL3_FLUSH_PREF,
    CAMSCOPE_HAL3_GET_CAM_INFO,
    CAMSCOPE_HAL3_DYN_UPDATE_META_STRM_INFO,
    CAMSCOPE_HAL3_CH_START,
    CAMSCOPE_HAL3_CH_STOP,
    CAMSCOPE_HAL3_CH_FLUSH,
    CAMSCOPE_HAL3_PROC_CH_STRM_CB,
    CAMSCOPE_HAL3_PROC_CH_REG_BUF,
    CAMSCOPE_HAL3_PROC_CH_REPROC_CB,
    CAMSCOPE_HAL3_REG_CH_INIT,
    CAMSCOPE_HAL3_REG_CH_REQ,
    CAMSCOPE_HAL3_METADATA_CH_INIT,
    CAMSCOPE_HAL3_RAW_CH_STRM_CB,
    CAMSCOPE_HAL3_YUV_CH_INIT,
    CAMSCOPE_HAL3_YUV_CH_STRM_CB,
    CAMSCOPE_HAL3_PIC_CH_JPEG_EVT_HANDLE,
    CAMSCOPE_HAL3_PIC_CH_REQ,
    CAMSCOPE_HAL3_PIC_CH_DATA_NOTIFY_CB,
    CAMSCOPE_HAL3_PIC_CH_STRM_CB,
    CAMSCOPE_HAL3_REPROC_CH_REG_BUF,
    CAMSCOPE_HAL3_REPROC_CH_START,
    CAMSCOPE_HAL3_REPROC_CH_STOP,
    CAMSCOPE_HAL3_PPROC_INIT,
    CAMSCOPE_HAL3_PPROC_INIT_JPEG,
    CAMSCOPE_HAL3_PPROC_REL_JPEG_JOB_DATA,
    CAMSCOPE_HAL3_PPROC_REL_PP_JOB_DATA,
    CAMSCOPE_HAL3_PPROC_ENCODEDATA,
    CAMSCOPE_HAL3_PREVIEW_STRM_CB,
    CAMSCOPE_HAL3_STOP_PREVIEW,
    CAMSCOPE_HAL3_CAPTURE_CH_CB,
    CAMSCOPE_EVENT_NAME_SIZE
} camscope_event_name;

extern const char * camscope_atrace_names[CAMSCOPE_EVENT_NAME_SIZE];

typedef struct {
    uint32_t packet_type;
    uint32_t size;
} camscope_base;

typedef struct {
    camscope_base base;
    struct timeval timestamp;
    int32_t thread_id;
    uint32_t event_name;
} camscope_sw_base;

typedef struct {
    camscope_sw_base sw_base;
    uint32_t frame_id;
} camscope_timing;

typedef struct {
    camscope_sw_base sw_base;
    struct timeval in_timestamp;
    struct timeval out_timestamp;
    uint32_t frame_id;
} camscope_in_out_timing;

void camscope_base_log(uint32_t camscope_section,
                       uint32_t camscope_enable_mask,
                       uint32_t packet_type);

void camscope_sw_base_log(uint32_t camscope_section,
                          uint32_t camscope_enable_mask, uint32_t packet_type,
                          uint32_t event_name);

void camscope_timing_log(uint32_t camscope_section,
                         uint32_t camscope_enable_mask, uint32_t packet_type,
                         uint32_t event_name, uint32_t frame_id);

void camscope_in_out_timing_log(uint32_t camscope_section,
                                uint32_t camscope_enable_mask,
                                uint32_t packet_type, uint32_t event_name,
                                struct timeval in_timestamp,
                                struct timeval out_timestamp,
                                uint32_t frame_id);

#endif /* __CAMSCOPE_PACKET_TYPE_H__ */
